<div class="layui-card">
    <div class="layui-card-body">


        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">{{ __('user.role_grant_menu') }}</li>
                <li class="">{{ __('user.role_grant_rule') }}</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-row">
                        <div class="layui-col-md4 layui-col-sm-offset4">
                            <div id="menu-tree"></div>
                            <div class="mt10 ml40">
                                <button class="layui-btn layui-btn-normal layui-btn-sm save-role-grant">
                                    {{ __('btn.save') }}
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-tab-item ">

                    {% include 'permission/rules.twig' %}

                    <div class="mt10 text-center">
                        <button class="layui-btn layui-btn-normal layui-btn-sm save-role-rules">
                            {{ __('btn.save') }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    layui.use(function () {

        let tree = layui.tree;
        let toast = layui.toast;
        let form = layui.form;
        let http = layui.http;
        let menus = JSON.parse('{{ menus|json_encode|raw }}');
        let id = '{{ id }}';

        tree.render({
            elem: '#menu-tree',
            data: menus,
            showCheckbox: true,  // 是否显示复选框
            onlyIconControl: true,  // 是否仅允许节点左侧图标控制展开收缩
            id: 'demo-id-1',
            accordion: true,
            isJump: true, // 是否允许点击节点时弹出新窗口跳转
            click: function (obj) {
                let data = obj.data;
            },
            oncheck: function (obj) {

            }
        });

        $('.save-role-grant').on('click', function () {
            let menu_ids = [];
            let choose = tree.getChecked('demo-id-1');
            for (let i in choose) {
                menu_ids.push(choose[i]['id']);
                let children = choose[i]['children'];
                for (let c in children) {
                    menu_ids.push(children[c]['id']);
                }
            }
            if (menu_ids.length === 0) {
                toast.top('请选择菜单');
                return false;
            }

            http.post('{{ auth_route('permission/saveRoleMenus') }}', {
                id: id,
                menu_ids: menu_ids
            }).then(res => {
                toast.top(res);
            });
        });

        let checked_rule_ids = JSON.parse('{{ has_rules|json_encode|raw }}');
        form.on('checkbox(rules)', function (data) {
            let elem = data.elem;
            let index = checked_rule_ids.indexOf(elem.value);
            if (elem.checked) {
                if (index === -1) {
                    checked_rule_ids.push(elem.value);
                }
            } else {
                if (index >= 0) {
                    checked_rule_ids.splice(index, 1);
                }
            }
        });

        $('.save-role-rules').on('click', function () {

            if (checked_rule_ids.length === 0) {
                toast.top('请选择权限');
                return false;
            }

            http.post('{{ auth_route('permission/saveRoleRules') }}', {
                id: id,
                rule_ids: checked_rule_ids
            }).then(res => {
                toast.top(res);
            });
        });

        $('.help-document').on('click', function () {
            layer.open({
                type: 1,
                title: '菜单跟权限说明',
                content: $(this).find('template').html()
            });
        });
    });
</script>
